angular.module('address-list',[])

.controller 'AddressListCtrl', ($scope,$http,$state,$ionicHistory,$ionicScrollDelegate,userData,$localStorage) ->
    $scope.$storage = $localStorage
    console.log $scope.$storage

    $http.get '/seller',params:{action:'shopaddresslist',userid:userData.getToken(),page:1,pagesize:$scope.pagesize}
    .success (resp) ->
        if resp.status == '0'
            console.log resp.body
            $scope.allpage=Number(resp.allpage) if Number(resp.allpage)>0
            $scope.addrlist=resp.body
            $scope.$storage.addrlist = $scope.addrlist

    $scope.$on '$ionicView.enter',->
        $scope.page=1
        $scope.allpage=1
    _.extend $scope,
        pagesize:100
        goBack: ->
#            $ionicHistory.goBack()
            $state.go 'tab.myself'
        goAddressAdd:->
            $state.go 'tab.edit-address-myself',{origin:'address-list'}
            $localStorage.set 'addressInfo', {
                id:'0'
                reciveuser:''
                phonenumber:''
                address:''
                remark:''
            }
        goEdit:(item)->
            $state.go 'tab.edit-address-myself',{origin:'address-list'}
            $localStorage.set 'addressInfo', {
                id:item.id
                reciveuser:item.reciveuser
                phonenumber:item.phonenumber
                address:item.address
                remark:item.remark
            }
        refreshData:()->
            $http.get '/seller',params:{action:'shopaddresslist',userid:userData.getToken(),page:$scope.page,pagesize:$scope.pagesize}
            .success (resp) ->
                if resp.status == '0'
                    console.log resp
                    $scope.addrlist=resp.body
        nextPage:->
            if $scope.page>=$scope.allpage
                return
            $scope.page=$scope.page+1
            $ionicScrollDelegate.$getByHandle().scrollTop()
            $scope.refreshData()
        prePage:->
            if $scope.page<=1
                return
            $scope.page=$scope.page-1
            $ionicScrollDelegate.$getByHandle().scrollTop()
            $scope.refreshData()